package com.bbt.dormassistant.controller;

import com.bbt.dormassistant.entity.ActivityRoom;
import com.bbt.dormassistant.entity.Record;
import com.bbt.dormassistant.service.ActivityRoomService;
import com.bbt.dormassistant.service.RecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "活动室预约接口")
@RestController
@RequestMapping("/record")

public class RecordController {

    @Autowired
    private RecordService recordService;
    @Autowired
    private ActivityRoomService activityRoomService;



    @ApiOperation(value = "添加新的记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "活动室ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "date", value = "日期(例：2019:8:11)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "begin", value = "起始时间(例：8:30 )", required = true, dataType = "String"),
            @ApiImplicitParam(name = "end", value = "结束时间(例：23:30)", required = true, dataType = "String"),
            @ApiImplicitParam(name = "userId", value = "申请人ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "reason", value = "理由", dataType = "String"),
    })
    @PostMapping("/addRecord")
    public Map<String,Object> addRecord(@RequestBody Record record){
        Map<String,Object> result = new HashMap<>();
        recordService.addRecord(record);
        result.put("success",1);
        return result;
    }


    @ApiOperation(value = "查询记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currIndex", value = "当前页码(可选项)", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小(可选项)", dataType = "int")
    })
    @GetMapping("/queryAll")
    public Map queryAll(@RequestParam(value = "currIndex",required = false) Integer currIndex,
                        @RequestParam(value = "pageSize",required = false) Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        List<Record> recordList=recordService.queryAll(currIndex,pageSize);
        map.put("records",recordList);
        List<ActivityRoom> activityRoomList=new ArrayList<>();
        for(int i=0;i<recordList.size();i++){
            activityRoomList.add(activityRoomService.queryActivityRoomById(recordList.get(i).getRoomId()));
        }
        map.put("roomList",activityRoomList);
        return map;
    }





    @ApiOperation(value = "根据userID获取用户所有的记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "currIndex", value = "当前页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int")
    })
    @GetMapping("/queryRecordByUserId")
    public Map queryRecordByUserId(@RequestParam(value = "userId") String userId,
                                   @RequestParam(value = "currIndex",required = false) Integer currIndex,
                                   @RequestParam(value = "pageSize",required = false) Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        List<Record> recordList=recordService.getRecordByUserId(userId,currIndex,pageSize);
        map.put("records",recordList);
        List<ActivityRoom> activityRoomList=new ArrayList<>();
        for(int i=0;i<recordList.size();i++){
            activityRoomList.add(activityRoomService.queryActivityRoomById(recordList.get(i).getRoomId()));
        }
        map.put("roomList",activityRoomList);
        return map;
    }



    @ApiOperation(value = "根据roomId获取该活动室所有的记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roomId", value = "房间ID", required = true, dataType = "int"),
            @ApiImplicitParam(name = "currIndex", value = "当前页码", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", dataType = "int")
    })
    @GetMapping("/queryRecordByRoomId")
    public Map queryRecordByRoomId(@RequestParam(value = "roomId") Integer roomId,
                                   @RequestParam(value = "currIndex",required = false) Integer currIndex,
                                   @RequestParam(value = "pageSize",required = false) Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("records",recordService.getRecordByRoomId(roomId,currIndex,pageSize));
        return map;
    }

    @GetMapping("/PassRecordApply")
    public Map passRecordApply(@RequestParam("recordId") Integer recordId){
        Record record=recordService.getRecordByRecordId(recordId);
        record.setIsApply((byte) 1);
        Map res=new HashMap();
        if(recordService.updateRecord(record)){
            res.put("success",1);
        }else {
            res.put("success",0);
        }
        return res;
    }


}
